package com.tencent.texttoimage.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.tencent.texttoimage.R;
import com.tencent.texttoimage.hook.QQHook;
import com.tencent.texttoimage.util.FileUtils;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        /**
         * 申请权限
         */
        int checkResult = getApplicationContext().checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        //if(!=允许),抛出异常
        if (checkResult != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); // 动态申请读取权限
            }
        } else {
        }

    }

    //菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.disIcon:

                new AlertDialog.Builder(this)
                .setTitle("警告")
                        .setMessage("隐藏之后，桌面图标不会再显示了，重新安装软件即可恢复。")
                        .setNeutralButton("取消",null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                PackageManager packageManager = getPackageManager();
                                ComponentName componentName = new ComponentName(MainActivity.this, MainActivity.class);
                                int res = packageManager.getComponentEnabledSetting(componentName);
                                if (res == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                                        || res == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                                    // 隐藏应用图标
                                    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                            PackageManager.DONT_KILL_APP);
                                    gohome();
                                } else {
                                    // 显示应用图标
                                    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
                                            PackageManager.DONT_KILL_APP);
                                }
                            }
                        })
                        .show();

                break;
            case R.id.clear:

                try {
                    FileUtils.delete("/sdcard/MessageToQR/");
                    Toast.makeText(this, "清除成功！", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(this, "没给权限？", Toast.LENGTH_SHORT).show();
                }

                break;
        }
        return super.onOptionsItemSelected(item);
    }


    private void gohome() {
        Intent intent2 = new Intent();
        // 为Intent设置Action、Category属性
        intent2.setAction(Intent.ACTION_MAIN);// "android.intent.action.MAIN"
        intent2.addCategory(Intent.CATEGORY_HOME); //"android.intent.category.HOME"
        startActivity(intent2);
    }


}
